/**
 * Copyright 2011 Mathieu Magnaudet
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * Copyright 2011 Mathieu Magnaudet
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package edu.enac.geoapp.gui.component;

import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.io.IOException;

import javax.swing.JInternalFrame;
import javax.swing.event.MouseInputListener;

import de.intarsys.pdf.example.viewer.awt.PDFCanvas;
import de.intarsys.pdf.parser.COSLoadException;
import de.intarsys.pdf.pd.PDDocument;
import de.intarsys.tools.locator.FileLocator;

@SuppressWarnings("serial")
public class TemsiInternalFrame extends JInternalFrame implements
		MouseWheelListener, MouseInputListener {
	private String pathname = null;
	private PDFCanvas pdfCanvas;
	private int x, y;
	private int dx;
	private int dy;

	public TemsiInternalFrame() {
		super("", true, // resizable
				true, // closable
				true, // maximizable
				true);// iconifiable

		pdfCanvas = new PDFCanvas();
		pdfCanvas.addMouseWheelListener(this);
		pdfCanvas.addMouseMotionListener(this);
		pdfCanvas.addMouseListener(this);

		add(pdfCanvas);

		setSize(800, 600);

		// Set the window's location.
		setLocation(0, 0);

	}

	public void updateDoc() {
		try {
			if (pathname != null)
				pdfCanvas.setDoc(getDoc());
		} catch (IOException e) {
			
		} catch (COSLoadException e) {
			
		}
	}

	private PDDocument getDoc() throws IOException, COSLoadException {
		FileLocator locator = new FileLocator(pathname);
		return PDDocument.createFromLocator(locator);
	}

	/**
	 * @param file
	 *            the file to set
	 */
	public void setPath(String path) {
		this.pathname = path;
	}

	/**
	 * @return the file
	 */
	public String getPath() {
		return pathname;
	}

	@Override
	public void mouseWheelMoved(MouseWheelEvent arg0) {
		if (arg0.getWheelRotation() == -1) {// zoom in
			pdfCanvas.setScale(pdfCanvas.getScale() * 1.2);
		} else {// zoom out
			pdfCanvas.setScale(pdfCanvas.getScale() / 1.2);
		}
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		dx = e.getX() - x;
		dy = e.getY() - y;
	}

	@Override
	public void mouseMoved(MouseEvent e) {

	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		x = e.getX();
		y = e.getY();
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		pdfCanvas.setLocation(pdfCanvas.getX() + dx, pdfCanvas.getY() + dy);
	}

}
